//
//  NSDictionary+Extension.m
//  YunDaHome-iOS
//
//  Created by 刘权 on 2020/1/15.
//  Copyright © 2020 YunDa. All rights reserved.
//

#import "NSDictionary+Extension.h"

@implementation NSDictionary (Extension)

/// 是否存在key
- (BOOL)j_hasKey:(id)key{
    return [self.allKeys containsObject:key];
}

/// 强制转换为string
- (NSString *)j_stringForKey:(id)key{
    if (![self j_hasKey:key]) {
        return @"";
    }
    id value = [self objectForKey:key];
    NSString *valueString = @"";
    if (kISNullObject(value)) {
        valueString = @"";
    }else{
        if ([value isKindOfClass:[NSString class]]) {
            valueString = (NSString *)value;
        }else if ([value isKindOfClass:[NSNumber class]]){
            NSNumber *numberValue = (NSNumber *)value;
            valueString = numberValue.stringValue;
        }
    }
    return valueString;
}

/// 安全地get number
- (NSNumber *)j_numberForKey:(id)key{
    if (![self j_hasKey:key]) {
        return @(0);
    }
    id value = [self objectForKey:key];
    if (kISNullObject(value)){
        return @(0);
    }
    if ([value isKindOfClass:[NSString class]]) {
        NSString *valueString = (NSString *)value;
        return [self numberWithString:valueString];
    }else if ([value isKindOfClass:[NSNumber class]]){
        return (NSNumber *)value;
    }
    return @(0);
}

/// 安全地get integer
- (NSInteger)j_integerForKey:(id)key{
    if (![self j_hasKey:key]) {
        return 0;
    }
    id value = [self objectForKey:key];
    if (kISNullObject(value)){
        return 0;
    }
    if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]){
        return [value integerValue];
    }
    return 0;
}

/// 安全地get float
- (float)j_floatForKey:(id)key{
    if (![self j_hasKey:key]) {
        return 0.0;
    }
    id value = [self objectForKey:key];
    if (kISNullObject(value)){
        return 0.0;
    }
    if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]){
        return [value floatValue];
    }
    return 0.0;
}

/// 安全地get CGPoint
- (CGPoint)j_pointForKey:(id)key{
    if (![self j_hasKey:key]) {
        return CGPointZero;
    }
    CGPoint point = CGPointFromString(self[key]);
    return point;
}

/// 安全地get CGSize
- (CGSize)j_sizeForKey:(id)key{
    if (![self j_hasKey:key]) {
        return CGSizeZero;
    }
    CGSize size = CGSizeFromString(self[key]);
    return size;
} 

/// 安全地get CGRect
- (CGRect)j_rectForKey:(id)key{
    if (![self j_hasKey:key]) {
        return CGRectZero;
    }
    CGRect rect = CGRectFromString(self[key]);
    return rect;
}

/// 将url参数转换成NSDictionary
+ (NSDictionary *)j_dictionaryWithURLQuery:(NSString *)query{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSArray *parameters = [query componentsSeparatedByString:@"&"];
    for(NSString *parameter in parameters) {
        NSArray *contents = [parameter componentsSeparatedByString:@"="];
        if([contents count] == 2) {
            NSString *key = [contents objectAtIndex:0];
            NSString *value = [contents objectAtIndex:1];
            value = [value stringByRemovingPercentEncoding];
            if (key && value) {
                [dict setObject:value forKey:key];
            }
        }
    }
    return [NSDictionary dictionaryWithDictionary:dict];
}

- (NSNumber *)numberWithString:(NSString *)string {
    NSString *str = [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString];
    if (!str || !str.length) {
        return nil;
    }
    
    static NSDictionary *dic;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dic = @{@"true" :   @(YES),
                @"yes" :    @(YES),
                @"false" :  @(NO),
                @"no" :     @(NO),
                @"nil" :    [NSNull null],
                @"null" :   [NSNull null],
                @"<null>" : [NSNull null]};
    });
    id num = dic[str];
    if (num) {
        if (num == [NSNull null]) return nil;
        return num;
    }
    
    // hex number
    int sign = 0;
    if ([str hasPrefix:@"0x"]) sign = 1;
    else if ([str hasPrefix:@"-0x"]) sign = -1;
    if (sign != 0) {
        NSScanner *scan = [NSScanner scannerWithString:str];
        unsigned num = -1;
        BOOL suc = [scan scanHexInt:&num];
        if (suc)
            return [NSNumber numberWithLong:((long)num * sign)];
        else
            return nil;
    }
    // normal number
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    return [formatter numberFromString:string];
}

@end

